<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:str="http://exslt.org/strings"
  extension-element-prefixes="str"
  version="1.0">

  <xsl:output method="html"/>
  <xsl:include href="ep_config.xsl"/>
  <xsl:param name="epc_targets"/>

  <xsl:template match="/action_metadata">
<!-- disabled SVG check until 2.0
    <script language="JavaScript" src="{$html_href}/static/js/svgcheck.js"></script>
    <script language="VBScript" src="{$html_href}/static/js/svgcheck.vbs"></script>
    <script language="JavaScript">checkAndGetSVGViewer();</script>
-->

    <script type="text/javascript">
      var cgi_href="<xsl:value-of select="$cgi_href"/>";
      var html_href="<xsl:value-of select="$html_href"/>";
    </script>
    <script type="text/javascript">//<![CDATA[
function seeobj ( obj ) {
  var s = "";

  for ( p in obj ) {
    if ( typeof ( obj[p] ) == "function" ) continue;
    s += p + ": " + obj[p] + "\n";
  }

  return s;
}

function ci(vg,Pf,yh) {
  this.type=vg;
  this.version=Pf;
  this.os=yh;
}

var m = new ci(0,0,null);
var Eb = navigator.userAgent.toLowerCase();
if ( Eb.indexOf ("msie") != -1 && document.all ) {
  m.type=1;
  if ( Eb.indexOf("msie 5") ) m.version=5;
} else if ( Eb.indexOf("safari") != -1 ) {
  m.type=3;
  if ( Eb.indexOf ("safari/125") !=-1 ) m.version=1;
} else if ( Eb.indexOf ("mozilla") != -1 ) {
  m.type=2;
} if ( Eb.indexOf ( "x11;" ) !=-1 ) {
  m.os=1;
}

function Xd() {};

Xd.create = function () {
  if ( m.type==1 ) {
    var Ud = m.version == 5 ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
    return new ActiveXObject(Ud);
  } else {
    return new XMLHttpRequest();
  }
}

function Y ( Vc ) {
  if ( m.type == 1 ) {
    this.stylesheet = Vc;
  } else if ( m.type != 3 ) {
    this.processor=new XSLTProcessor();
    this.processor.importStylesheet(Vc);
  }
}

Y.cache_=new Object();

Y.create=function(Vc) {
  return new Y(Vc);
}
 
Y.getCached=function(lb) {
  return Y.cache_[lb];
}

Y.cache = function (lb, Nf) {
  Y.cache_[lb]=Nf;
}

Y.prototype.transformToHTML=function(qc,vb,params) {
  if ( this.stylesheet ) {
    for ( param in params ) {
        // hackish... :)
      var p = this.stylesheet.selectSingleNode ( '/xsl:stylesheet/xsl:param[@name="' + param + '"]' );
      p.text = params[param];
    }

    vb.innerHTML=qc.transformNode(this.stylesheet);
  } else {
    for ( param in params ) {
      this.processor.setParameter ( null, param, params[param] );
    }
    var Uh=this.processor.transformToFragment(qc,window.document);
    vb.innerHTML="";
    vb.appendChild(Uh);
  }
}

// kc - stylesheet URL
// qc - xml to transform
// vb - where in document to place the results
// Nc - function to call after transform
// params - array of parameters
Y.asynchronousTransform=function(qc,vb,kc,Nc,params) {
  if(m.type==3) return;

  var cc=Y.getCached(kc);

  if (cc) {
    cc.transformToHTML(qc,vb,params);
    if(Nc) Nc();
    return;
  }

  var sa = Xd.create();
  sa.open ( "GET", kc, true );

  sa.onreadystatechange = function () {
    if ( sa.readyState==4 ) {
      try {
	var Db = sa.responseXML;
	var cc = Y.create ( Db );
	Y.cache ( kc, cc );
	cc.transformToHTML ( qc, vb, params );
	if(Nc) Nc();
      } catch(b) {}
    }
  };

  sa.send(null);
}

function render_table ( table_div_id, params ) {
  params.table_div_id = table_div_id;
  Y.asynchronousTransform ( document.getElementById(table_div_id).xml,document.getElementById(table_div_id), html_href + '/static/xsl/format_table.xsl', null , params );
}

function load_table ( table_div_id, meta_id ) {
    var sa = Xd.create();
    sa.open ( "GET", cgi_href + "/get_xml.pl?meta_id=" + meta_id, true );

    sa.onreadystatechange = function () {
    if ( sa.readyState==4 ) {
      try {
        document.getElementById ( table_div_id ).xml = sa.responseXML;
           render_table(table_div_id, {} );
         } catch(b) {}
        }
      };

      sa.send(null);
}
        //]]></script>

    <xsl:call-template name="epc_target_links"/>


    <h1>Results</h1>
	<table style="background-color:cream" width="90%" cellspacing="10"> 
<tr>
  <td>
        <xsl:if test="meta[@name='biplot_graph']">
              <xsl:apply-templates select="meta[@name='biplot_graph']"/> 
         <td>
                This is an overview plot of your analysis. Graphs A-C shows the first two eigenvectors.
                The first eigenvector is the horizontal axis, and the second eigenvector is the vertical axis.  T
                The greatest discimination is achieved by the 1st eigenvector, the next by the second and so on.
                The number of eigenvectors will be equal to the number of classes you defined - 1. 
                Graph A shows the resulting projection of the microarrays, Graph
                B shows the classes of microarrays and  Graph C shows the genes projection in these 2 dimensions (1st and  2nd eigenvector).
                If COA-BGA was run then the microarray samples
                will be projected onto the genes.  Graph D shows the eigenvalues.  The number of eigenvalues will be equal to the number
                of classes -1 and the number of eigenvectors.  The greater the eigenvalue the greater the variance (amount of information)
                described by the eigenvector.
         </td>

        </xsl:if>
</td></tr>
</table>

<table style="background-color:yellow" width="90%" cellspacing="10">
 
 <tr>
   <td colspan="2">
    <b><i> Arrays and Genes that are projected in the same direction from the origin, show genes 
     that are upregulated in those microarray sample</i></b>
   </td>
 </tr>

 <tr>
    <td>
        <xsl:if test="meta[@name='array_graph']">
               <xsl:apply-templates select="meta[@name='array_graph']"/>
                This graph is a larger version of graph A above.  The projection of the array samples on the first 2 eigenvectors are shown.
        </xsl:if>
    </td>
    <td>
	<xsl:if test="meta[@name='gene_graph']">
		<xsl:apply-templates select="meta[@name='gene_graph']"/>
                This graph is a larger version of graph C above. The projection of the array samples on the first 2 eigenvectors are shown.
	</xsl:if>
    </td>

 </tr>
</table>

<table  style="background-color:blue" width="90%" cellspacing="10">
<tr>
 <td>
 <xsl:template match="meta[@type='chime']">
    <h2><xsl:value-of select="@label"/></h2>
    <xsl:value-of select="." disable-output-escaping="yes"/>
     Rotate in first 3 axes
  </xsl:template>
</td>
</tr>
</table>

	<xsl:if test="meta[@name='cols']">
		Coordinates of the Arrays.
	</xsl:if>
        <xsl:if test="meta[@name='rows']">
                Coordinates of the Genes.
        </xsl:if>


    <xsl:apply-templates select="meta[@type!='input'][@label!='']"/>
    <xsl:apply-templates select="meta[@name='html']"/>

    <h1 onmouseover="this.style.cursor='pointer'" onclick="other_action_metadata.style.display=(other_action_metadata.style.display=='none'?'':'none')">Show other metadata >></h1>
    <div id="other_action_metadata" style="display:none">
      <xsl:apply-templates select="meta[@type!='input'][@name!='html'][@label='']"/>
      <xsl:apply-templates select="meta[@type='input']"/>
    </div>

    <xsl:call-template name="epc_target_links"/>
  </xsl:template>

  <xsl:template match="meta[@type='image/png']">
     <h2><xsl:value-of select="@label"/></h2>
     <img style="align:center" border='1' src="{text()}"/>
     <br/><br/>
   </xsl:template>


 <xsl:template match="meta[@type='xml/table']">
    <h2><xsl:value-of select="@label"/></h2>
    <xsl:variable name="table_div_id" select="generate-id(.)"/>
    <div id="{$table_div_id}"><span id="loading_{$table_div_id}" style="padding:3px;background-color:darkred;color:white">Loading...</span></div>
    <script type="text/javascript">
      load_table ( "<xsl:value-of select="$table_div_id"/>", "<xsl:value-of select="@id"/>" );
    </script>
  </xsl:template>

  <xsl:template match="meta[@type='text/plain']">
    <h2><xsl:value-of select="@label"/></h2>
    <iframe width="100%" height="200" src="{text()}"/>
  </xsl:template>

  <xsl:template match="meta[@type='number']">
    <table border="1" cellpadding="2" cellspacing="0">
      <tr>
        <td width="200"><xsl:value-of select="@label"/></td><td valign="middle" width="100"><xsl:value-of select="text()"/></td>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="meta[@type='chime']">
    <h2><xsl:value-of select="@label"/></h2>
    <xsl:value-of select="." disable-output-escaping="yes"/>
  </xsl:template>

  <xsl:template match="meta[@type='blank']">
    <span style="color:red;font-weight:bold">No results returned!</span>
  </xsl:template>

  <xsl:template match="meta[@name='html']">
    <script language="javascript">
<![CDATA[
    function
    resize_contentiframe( id )
        {
        f = document.getElementById(id);
        if (f.contentDocument)
            {
            fDocument = f.contentDocument; 
            }
        else if (f.contentWindow)
            { // For IE5.5 and IE6
            fDocument = f.contentWindow.document;
            }
        else if (f.document)
            { // For IE5
            fDocument = f.document;
            }
        else
            {
            return;
            }

        f.height = fDocument.body.scrollHeight + 100;
        f.width = fDocument.body.scrollWidth + 100;
        }
]]>
    </script>
    <iframe id="the_contentiframe" onload="resize_contentiframe('the_contentiframe')" width="100%" height="100%" src="{text()}"/>
  </xsl:template>

  <xsl:template match="meta">
    <xsl:choose>
      <xsl:when test="@label!=''">
        <div style="background-color:#CCCCCC;"><xsl:value-of select="@label"/>: <xsl:value-of select="text()"/></div>
      </xsl:when>
      <xsl:otherwise>
        <div style="background-color:#CCCCCC;"><xsl:value-of select="@name"/>: <xsl:value-of select="text()"/></div>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*"/>

  <xsl:template name="epc_target_links">
    <xsl:if test="$epc_targets">
      <div style="float:right">
        <b>Proceed to:</b>
        <xsl:for-each select="str:tokenize($epc_targets,':')">
          <xsl:variable name="target_comp_id"    select="str:tokenize(.,',')[1]"/>
          <xsl:variable name="target_comp_title" select="str:tokenize(.,',')[2]"/>
          
          <div style="text-align:left">
            <a class="follow-assoc" href="{$cgi_href}/ep_ui.pl?render_component_ids={$target_comp_id}">
              <b>>> <xsl:value-of select="$target_comp_title"/></b>
            </a>
          </div>
        </xsl:for-each>
      </div>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
